import { getStatementsList, doApproval, pay, getStatementById, getPayById, getApprovalById } from '../../services/capital/approval';
import { parse } from 'qs';
import { message } from 'antd';

const dateFormat = 'YYYY-MM-DD';

export default {
    namespace: 'pay',
    state: {
        search: { // 查询条件
            time: '',
            cstId: '',
            stateno: '',
            status: '2',
            beginTime: '',
            endTime: '',
            minamount: '',
            maxamount: '',
            creator: '',

        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            defaultWidth: 800, // 缺省宽度800
            width: 800, // 宽度
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '结算单审核', // 标题
            step: 1,
            form: {
            }
        }
    },


    effects: {
        // 分页查询结算单列表
        *getStatementsList({ }, { call, put, select }) {
            const search = yield select(state => state.pay.search);
            const page = yield select(state => state.pay.page);

            if (search.time != null && search.time != undefined) {
                if (search.time.length >= 2) {
                    if (search.time[0] != '') {
                        search.beginTime = search.time[0].format(dateFormat);
                    }
                    if (search.time[1] != '') {
                        search.endTime = search.time[1].format(dateFormat);
                    }

                }
            }

            const res = yield call(getStatementsList, {
                status: search.status,
                stateno: search.stateno,
                cstId: search.cstId,
                beginTime: search.beginTime,
                endTime: search.endTime,
                minamount: search.minamount,
                maxamount: search.maxamount,
                creator: search.creator,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page == null ? page : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        // 结算单详情
        *getStatementById({ payload }, { call, put, select }) {
            const res = yield call(getStatementById, { id: payload.id });
            if (res.data.resultCode === "ok") {
                let content = res.data.content.statement;


                let type = "setEditModel";
                let status = 4;
                if (content != null) {
                    status = content.status;
                    if (status == 5 || status == 6) {
                        type = "getPayById";
                    }
                }

                yield put({
                    type: type,
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        step: payload.step,
                        title: payload.title,
                        id: content.id,
                        form: {
                            id: content.id,
                            stateno: content.stateno,
                            cstid: content.cstid,
                            transferamount: content.transferamount,
                            unpayamount: content.unpayamount,
                            filename: content.filename,
                            url: content.url,
                            accname: content.accname,
                            accopenbank: content.accopenbank,
                            accno: content.accno,
                            payman: content.payman,
                            paytime: content.paytime,
                            creator: content.creator,
                            createtime: content.createtime,
                            status: content.status,
                            memo: content.memo,
                            type: payload.type,
                        }
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        //付款详情
        *getPayById({ payload }, { call, put, select }) {
            const res = yield call(getPayById, { id: payload.id });
            if (res.data.resultCode === "ok") {

                let content = res.data.content;

                let form = payload.form;

                if (content != null) {
                    form.payUrl = content[0].url;
                    form.payMemo = content[0].memo;
                    form.payFilename = content[0].filename;
                }

                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        step: payload.step,
                        title: payload.title,
                        form: payload.form,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        // 付款
        *doPay({ payload }, { call, put, select }) {
            const res = yield call(pay, payload);
            if (res.data.resultCode === "ok") {
                message.info("付款成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getStatementsList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    },

    reducers: {
        // 设置查询条件
        setSearch(state, action) {
            return { ...state, search: action.payload.search };
        },

        // 是否显示
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = { ...state.editModel };
            if (payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if (payload.width != undefined) {
                newState.width = payload.width;
            }
            if (payload.title != undefined) {
                newState.title = payload.title;
            }
            if (payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if (payload.step != undefined) {
                newState.step = payload.step;
            }

            if (payload.type != undefined) {
                newState.type = payload.type;
            }
            if (payload.form != undefined) {
                newState.form = payload.form;
            }
            return { ...state, editModel: { ...newState } };
        },


        // 设置数据列表
        setList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },

        // 设置页码
        setPage(state, { payload }) {
            return { ...state, page: payload.page }
        }

    },


    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                dispatch({
                    type: 'getStatementsList'
                });
            });
        }
    }
};
